// API服务文件
const BASE_URL = 'https://api.example.com' // 替换为实际的API地址

// 请求封装
const request = (url, options = {}) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			method: options.method || 'GET',
			data: options.data || {},
			header: {
				'Content-Type': 'application/json',
				...options.header
			},
			success: (res) => {
				if (res.statusCode === 200) {
					resolve(res.data)
				} else {
					reject(new Error(res.data.message || '请求失败'))
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

// 商城首页API
export const mallApi = {
	// 获取首页数据
	getHomeData: () => {
		return request('/mall/home')
	},
	
	// 获取分类商品
	getCategoryProducts: (categoryId, page = 1, size = 10) => {
		return request('/mall/products', {
			method: 'GET',
			data: { categoryId, page, size }
		})
	},
	
	// 搜索商品
	searchProducts: (keyword, page = 1, size = 10) => {
		return request('/mall/search', {
			method: 'GET',
			data: { keyword, page, size }
		})
	},
	
	// 获取轮播图
	getBanners: () => {
		return request('/mall/banners')
	},
	
	// 获取国家补贴商品
	getSubsidyProducts: () => {
		return request('/mall/subsidy-products')
	},
	
	// 获取包邮商品
	getShippingProducts: () => {
		return request('/mall/shipping-products')
	},
	
	// 获取推荐商品
	getRecommendProducts: (page = 1, size = 10) => {
		return request('/mall/recommend-products', {
			method: 'GET',
			data: { page, size }
		})
	}
}

// 用户相关API
export const userApi = {
	// 获取用户位置
	getUserLocation: () => {
		return new Promise((resolve, reject) => {
			uni.getLocation({
				type: 'gcj02',
				success: resolve,
				fail: reject
			})
		})
	},
	
	// 获取用户信息
	getUserInfo: () => {
		return request('/user/info')
	}
}

// 活动相关API
export const activityApi = {
	// 获取秒杀活动
	getFlashSale: () => {
		return request('/activity/flash-sale')
	},
	
	// 获取团购活动
	getGroupBuy: () => {
		return request('/activity/group-buy')
	},
	
	// 获取签到信息
	getCheckinInfo: () => {
		return request('/activity/checkin')
	}
}

// 商品相关API
export const productApi = {
	// 获取商品详情
	getProductDetail: (productId) => {
		return request(`/product/${productId}`)
	},
	
	// 获取商品评价
	getProductReviews: (productId, page = 1, size = 10) => {
		return request(`/product/${productId}/reviews`, {
			method: 'GET',
			data: { page, size }
		})
	},
	
	// 添加商品到购物车
	addToCart: (productId, quantity = 1) => {
		return request('/cart/add', {
			method: 'POST',
			data: { productId, quantity }
		})
	}
}

// 模拟数据（用于开发测试）
export const mockData = {
	// 模拟首页数据
	getHomeData: () => {
		return new Promise((resolve) => {
			setTimeout(() => {
				resolve({
					banners: [
						{ 
							id: 1,
							image: '/static/banner1.jpg', 
							title: '爆品好价', 
							subtitle: '抢百万红包雨', 
							link: '/pages/activity/flash' 
						},
						{ 
							id: 2,
							image: '/static/banner2.jpg', 
							title: '直播低价', 
							subtitle: '抢0.01元秒杀', 
							link: '/pages/activity/live' 
						}
					],
					categories: [
						{ id: 'recommend', name: '推荐' },
						{ id: 'takeaway', name: '品质外卖' },
						{ id: 'subsidy', name: '国家补贴' },
						{ id: 'appliance', name: '大家电' },
						{ id: 'phone', name: '手机' },
						{ id: 'computer', name: '电脑' }
					],
					subsidyProducts: [
						{
							id: 1,
							image: '/static/phone.jpg',
							subsidy: '313.6',
							price: '6299',
							title: '华为Mate70 Pro'
						},
						{
							id: 2,
							image: '/static/fridge.jpg',
							subsidy: '21.1',
							price: '269.9',
							title: '海尔冰箱'
						}
					],
					shippingProducts: [
						{
							id: 3,
							image: '/static/umbrella.jpg',
							shippingText: '便宜包邮',
							title: '雨伞'
						},
						{
							id: 4,
							image: '/static/eggs.jpg',
							price: '5.2',
							title: '新鲜鸡蛋'
						}
					],
					products: [
						{
							id: 5,
							image: '/static/ace-phone.jpg',
							price: '2099',
							title: 'Ace手机'
						},
						{
							id: 6,
							image: '/static/gaming-mouse.jpg',
							price: '799',
							title: '游戏鼠标'
						},
						{
							id: 7,
							image: '/static/gold-phone.jpg',
							price: '3999',
							title: '金色手机'
						},
						{
							id: 8,
							image: '/static/msi-gpu.jpg',
							price: '2999',
							title: 'MSI显卡'
						}
					],
					pcProducts: [
						{
							id: 9,
							image: '/static/gaming-pc.jpg',
							price: '6.88',
							title: '2025款i9级18核4060台...',
							desc: '京东物流 包邮 免费上门退换',
							redPacket: '现金群红包'
						}
					]
				})
			}, 500)
		})
	},
	
	// 模拟搜索商品
	searchProducts: (keyword) => {
		return new Promise((resolve) => {
			setTimeout(() => {
				resolve({
					products: [
						{
							id: 10,
							image: '/static/search-result.jpg',
							price: '1999',
							title: `搜索结果: ${keyword}`
						}
					],
					total: 1
				})
			}, 300)
		})
	}
}

export default {
	request,
	mallApi,
	userApi,
	activityApi,
	productApi,
	mockData
}
